package CutLoad.components
{
	import CutLoad.Base;
	import CutLoad.components.container.ContainerH;
	import CutLoad.components.container.ContainerV;
	import CutLoad.cookies.Painter;
	
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * 调色面板 
	 * @author smallerbird
	 * 
	 */	
	public class CColorPicker extends Base
	{
		private var colorTileWH:Number=15;
		private var body:CButton
		private var spBak:Sprite
		private var txt:CTextInput
		private var rect:Rectangle=null;
		//色板
		private var container:ContainerV
		//当点击确定某一个色时
		private var funClick:Function=null;
		//当鼠标划过某一个色块时
		private var funChange:Function=null;
		
		private var currentColor:Number;
		
		private var colorArr:Array = [
			[0x000000, 0x003300, 0x006600, 0x009900, 0x00cc00, 0x00ff00 ,0x330000,0x333300,0x336600,0x339900,0x33cc00,0x33ff00,0x660000,0x663300,0x666600,0x669900,0x66cc00,0x66ff00],
			[0x000033, 0x003333, 0x006633, 0x009933, 0x00cc33, 0x00ff33 ,0x330033,0x333333,0x336633,0x339933,0x33cc33,0x33ff33,0x660033,0x663333,0x666633,0x669933,0x66cc33,0x66ff33],
			[0x000066, 0x003366, 0x006666, 0x009966, 0x00cc66, 0x00ff66 ,0x330066,0x333366,0x336666,0x339966,0x33cc66,0x33ff66,0x660066,0x663366,0x666666,0x669966,0x66cc66,0x66ff66],
			[0x000099, 0x003399, 0x006699, 0x009999, 0x00cc99, 0x00ff99 ,0x330099,0x333399,0x336699,0x339999,0x33cc99,0x33ff99,0x660099,0x663399,0x666699,0x669999,0x66cc99,0x66ff99],
			[0x0000cc, 0x0033cc, 0x0066cc, 0x0099cc, 0x00cccc, 0x00ffcc ,0x3300cc,0x3333cc,0x3366cc,0x3399cc,0x33cccc,0x33ffcc,0x6600cc,0x6633cc,0x6666cc,0x6699cc,0x66cccc,0x66ffcc],
			[0x0000ff, 0x0033ff, 0x0066ff, 0x0099ff, 0x00ccff, 0x00ffff ,0x3300ff,0x3333ff,0x3366ff,0x3399ff,0x33ccff,0x33ffff,0x6600ff,0x6633ff,0x6666ff,0x6699ff,0x66ccff,0x66ffff],
			[0x990000, 0x993300, 0x996600, 0x999900, 0x99cc00, 0x99ff00 ,0xcc0000,0xcc3300,0xcc6600,0xcc9900,0xcccc00,0xccff00,0xff0000,0xff3300,0xff6600,0xff9900,0xffcc00,0xffff00],
			[0x990033, 0x993333, 0x996633, 0x999933, 0x99cc33, 0x99ff33 ,0xcc0033,0xcc3333,0xcc6633,0xcc9933,0xcccc33,0xccff33,0xff0033,0xff3333,0xff6633,0xff9933,0xffcc33,0xffff33],
			[0x990066, 0x993366, 0x996666, 0x999966, 0x99cc66, 0x99ff66 ,0xcc0066,0xcc3366,0xcc6666,0xcc9966,0xcccc66,0xccff66,0xff0066,0xff3366,0xff6666,0xff9966,0xffcc66,0xffff66],
			[0x990099, 0x993399, 0x996699, 0x999999, 0x99cc99, 0x99ff99 ,0xcc0099,0xcc3399,0xcc6699,0xcc9999,0xcccc99,0xccff99,0xff0099,0xff3399,0xff6699,0xff9999,0xffcc99,0xffff99],
			[0x9900cc, 0x9933cc, 0x9966cc, 0x9999cc, 0x99cccc, 0x99ffcc ,0xcc0066,0xcc33cc,0xcc66cc,0xcc99cc,0xcccccc,0xccffcc,0xff00cc,0xff33cc,0xff66cc,0xff99cc,0xffcccc,0xffffcc],
			[0x9900ff, 0x9933ff, 0x9966ff, 0x9999ff, 0x99ccff, 0x99ffff ,0xcc00ff,0xcc33ff,0xcc66ff,0xcc99ff,0xccccff,0xccffff,0xff00ff,0xff33ff,0xff66ff,0xff99ff,0xffccff,0xffffff]
		];
		/**
		 * 初始化 
		 * @param iniColor 初始时显示色
		 * @param colorTileWH 调色面板，第一个色块的宽高
		 * @param bakColor 调色面板背景色
		 * 
		 */		
		public function init(iniColor:Number=0x000000,colorTileWH:Number=15,bakColor:Number=0xffffff):void{
			this.colorTileWH=colorTileWH;
			currentColor=iniColor;
			body=new CButton();
			var arrSkin:Array=Skin.getInstance().getSkin(10)
			body.setSkin(arrSkin);
			body.init();
			
			spBak=new Sprite();
			this.addChild(spBak);
			
			this.setBodyBakColor(iniColor);
			
			body.show(this);
			body.addEventListener(MouseEvent.CLICK,click);
			//
			container=new ContainerV();
			container.init();
			txt=new CTextInput();
			txt.init();
			txt.setWH(100);
			txt.text="#"+iniColor.toString(16);
			container.append(txt,false);
			
			
			var containerC:ContainerH=new ContainerH();
			var temWidth:Number
			temWidth=colorArr[0].length*colorTileWH+colorArr[0].length
			containerC.init({w:temWidth,isAuto:true,xspace:0,yspace:0});
			for(var i:Number=0;i<colorArr.length;i++){
				for(var j:Number=0;j<colorArr[i].length;j++){
					var base:Base=Painter.drawRect(colorTileWH,colorTileWH,colorArr[i][j],1,0,0,null,1,0xffffff,0.5);
					base.mouseChildren=false;
					base.appendOtherInfo({colorValue:colorArr[i][j]});
					base.addEventListener(MouseEvent.MOUSE_OVER,over);
					base.addEventListener(MouseEvent.CLICK,clickColor);
					containerC.append(base);
				}
			}
			containerC.setTakeWH();
			//
			container.append(containerC);
			container.setTakeWH();
			var baseBakBak:Base=Painter.drawRect(container.getShowWidth(),container.getShowHeight(),bakColor);
			container.addChild(baseBakBak);
			container.swapChildren(container.getChildeParent(),baseBakBak);
		}
		private function setBodyBakColor(c:Number):void{
			var gg:Graphics=spBak.graphics;
			gg.clear();
			gg.beginFill(c);
			gg.drawRect(0,0,this.getShowWidth(),this.getShowHeight());
			gg.endFill()
			//
			currentColor=c;
		}
		
		private function clickColor(e:MouseEvent):void{
			if(funClick!=null) funClick(currentColor);
			this.close();
		}
		private var overBaseNow:Base=null;
		private function over(e:MouseEvent):void{
			var base:Base=e.target as Base;
			if(overBaseNow!=null) overBaseNow.alpha=1;
			base.alpha=0
			overBaseNow=base;
			
			var cc:Number=base.getOtherInfo().colorValue
			txt.text="#"+String(cc.toString(16))
			setBodyBakColor(cc);
			//
			if(funChange!=null) funChange(cc)
		}
		private function click(e:MouseEvent):void{
			this.open()
		}
		/**
		 * 获得当前色值 
		 * @return 
		 * 
		 */		
		public function get value():Number{
			return currentColor;
		}
		/**
		 * 当点击确定时调用 
		 * @param fun 格式:function(color)
		 * 
		 */		
		public function setFunClick(fun:Function):void{
			funClick=fun;
		}
		/**
		 * 当鼠标划过调色板色块是调用 
		 * @param fun 格式:function(color)
		 * 
		 */		
		public function setFunChange(fun:Function):void{
			funChange=fun;
		}
		/**
		 * 打开调色面板 
		 * 
		 */
		public function open():void{
			var pp:Point=body.localToGlobal(new Point());
			rect=new Rectangle(pp.x,pp.y,container.width+body.getShowWidth(),container.height);
			container.setXY(pp.x+body.getShowWidth(),pp.y);
			body.stage.addChild(container);
			body.addEventListener(Event.ENTER_FRAME,enterFrame);
		}
		/**
		 * 关闭调色面板 
		 * 
		 */		
		public function close():void{
			body.stage.removeChild(container);
			body.removeEventListener(Event.ENTER_FRAME,enterFrame);
			rect=null;
		}
		private function enterFrame(e:Event):void{
			if(rect!=null){
				if(!rect.contains(body.stage.mouseX,body.stage.mouseY)){
					close()
				}
			}
		}
		override public function killMe():void{
			if(body.hasEventListener(Event.ENTER_FRAME)){
				body.removeEventListener(Event.ENTER_FRAME,enterFrame);
			}
			body.removeEventListener(MouseEvent.CLICK,click);
			super.killMe();
		}
		override public function getShowWidth():Number{
			return body.getShowWidth()
		}
		override public function getShowHeight():Number{
			return body.getShowHeight();	
		}
		
	}
}